﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using InformationExtraction.Common;
using InformationExtraction.Interface;

namespace InformationExtraction.Utils
{
    //cfj
    class NotificationDateExtracter : IFeatureExtractable
    {
        #region IFeatureExtractable 成员

        List<int> IFeatureExtractable.Extract(Token token)
        {
            List<int> fList = new List<int>();
            string word = token.Content;
            string line = token.InLine.Content;

            fList.Add(GetMonth(word));
            fList.Add(GetDay(word));
            fList.Add(GetYear(word));
            fList.Add(GetStandardFormat(word));

            fList.Add(GetNotificationSpecified(line));
            fList.Add(GetMonthNearBy(line));
            fList.Add(GetKeyValue(line));
            return fList; 
        }

        InformationType IFeatureExtractable.GetTargetType()
        {
            return InformationType.NotificationDate;
            //throw new NotImplementedException();
        }

        #endregion

        #region private functions
        /// <summary>
        /// 提取第一个特征值
        /// 方法：是否是十二个月中的任何一个
        /// </summary>
        /// <param name="text"></param>
        /// <returns>是返回1，不是返回-1</returns>
        private int GetMonth(string word)
        {
            Regex r = new Regex(@"(^|\W)((January)|(February)|(March)|(April)|(May)|(June)|(July)|(August)|(September)|(October)|(November)|(December)|(Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec))($|\W)", RegexOptions.IgnoreCase);
            if (r.IsMatch(word))
                return 1;

            return -1;
        }

        /// <summary>        
        /// 方法：是否使介于1-31之间的数字
        /// </summary>
        /// <param name="word"></param>
        /// <returns>是返回1，不是返回-1</returns>
        private int GetDay(string word)
        {
            Regex r = new Regex(@"^(0?\d|[12]\d|3[01])(st)?(rd)?(nd)?(th)?(,)?$");
            //Regex r = new Regex(@"^(0?\d|[12]\d|3[01])(st)?(rd)?(nd)?(th)?$");
            if (r.IsMatch(word))
                return 1;
            else
                return -1;
        }

        /// <summary>
        /// 提取第四个特征值
        /// 方法：是否是介于1900-2099间的年份
        /// </summary>
        /// <param name="word"></param>
        /// <returns>是返回1，不是返回-1</returns>
        private int GetYear(string word)
        {
            Regex r = new Regex(@"^\W*(19|20)\d{2}\W*$");
            if (r.IsMatch(word))
                return 1;
            else
                return -1;
        }

        // 是否有Notification Attributes的明显字样
        private int GetNotificationSpecified(string line)
        {
            Regex r = new Regex(@"(notification)|(notified)|(author)|(letter)", RegexOptions.IgnoreCase);
            if (r.IsMatch(line))
                return -1;
            return 1;
        }

        //所在行是否有Month
        private int GetMonthNearBy(string line)
        {
            Regex r = new Regex(@"(January)|(February)|(March)|(April)|(May)|(June)|(July)|(August)|(September)|(October)|(November)|(December)|(Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec)", RegexOptions.IgnoreCase);

            if (r.IsMatch(line))
                return 1;
            else
                return -1;
        }
        //是否是标准格式的日期
        private int GetStandardFormat(string word)
        {
            DateTime date;
            if (DateTime.TryParse(word, out date))
                return 1;
            else
                return -1;
        }

        //是否 Notification date :YYYY格式 且包含月份
        private int GetKeyValue(string line)
        {            
            if (line != null || line != "")
            {
                if (line.Contains(":") && GetMonthNearBy(line) > 0)
                {
                    string[] ls = line.Split(':');
                    if (ls.Length == 2)
                    {
                        if (GetNotificationSpecified(ls[0]) == 1)
                            return 1;
                    }
                }
                return -1;
            }                
            return -1;
        }



        # endregion

    }
}
